package com.flyxiaozhu.erp.core.services;

import com.flyxiaozhu.erp.core.entities.GoodsOrder;
import com.flyxiaozhu.erp.core.entities.GoodsOrderDetail;
import com.flyxiaozhu.erp.core.repositories.GoodsOrderDetailRepository;
import com.flyxiaozhu.erp.core.repositories.GoodsOrderRepository;
import com.flyxiaozhu.erp.core.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author flyxiaozhu
 * @date 2019-04-26 23:33
 */
@Service
public class GoodsOrderService {
    @Autowired
    GoodsOrderRepository goodsOrderRepository;
    @Autowired
    GoodsOrderDetailRepository goodsOrderDetailRepository;

    public GoodsOrder findById(long id) {
        return goodsOrderRepository.findById(id).orElse(null);
    }

    public Map<Long, List<GoodsOrderDetail>> findByOrderIdGroup() {
        List<GoodsOrderDetail> orderDetails = goodsOrderDetailRepository.findAll();
        return CollectionUtils.group(orderDetails, new CollectionUtils.GroupFilter<GoodsOrderDetail, Long, GoodsOrderDetail>() {
            @Override
            public Long getKey(GoodsOrderDetail source) {
                return source.getOrderId();
            }

            @Override
            public GoodsOrderDetail getValue(GoodsOrderDetail source) {
                return source;
            }
        });
    }
}
